package com.example.controller;


import com.example.controller.request.ShortLinkAddRequest;
import com.example.controller.request.ShortLinkDelRequest;
import com.example.controller.request.ShortLinkPageRequest;
import com.example.controller.request.ShortLinkUpdateRequest;
import com.example.service.ShortLinkService;
import com.example.utils.JsonData;
import com.example.vo.ShortLinkVO;
import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yinxin
 * @since 2022-06-08
 */
@RestController
@RequestMapping("/api/link/v1")
public class ShortLinkController {

    @Autowired
    ShortLinkService shortLinkService;

    @Value("${rpc.token}")
    private String rpcToken;

    /**
     * 检查短链是否存在
     * @param shortLinkCode
     * @return
     */
    @GetMapping( "/check")
    public JsonData check(@RequestParam("shortLinkCode")String shortLinkCode, HttpServletRequest request){
        String reqToken = request.getHeader("rpc-token");
        if(rpcToken.equalsIgnoreCase(reqToken)){
            ShortLinkVO shortLinkVO = shortLinkService.parseShortLinkCode(shortLinkCode);
            return shortLinkVO==null?JsonData.buildError("短链不存在"):JsonData.buildSuccess();
        }else {
            return JsonData.buildError("非法访问");
        }
    }

    /**
     * 创建短链接口
     * @param request
     * @return
     */
    @PostMapping("/add")
    public JsonData createShortLink(@RequestBody ShortLinkAddRequest request){
        return shortLinkService.createShortLink(request);
    }

    /**
     * 删除短链接口
     * @param request
     * @return
     */
    @PostMapping("/del")
    public JsonData del(@RequestBody ShortLinkDelRequest request){
        return shortLinkService.del(request);
    }

    /**
     * 修改短链接口
     * @param request
     * @return
     */
    @PostMapping("/update")
    public JsonData update(@RequestBody ShortLinkUpdateRequest request){
        return shortLinkService.update(request);
    }

    /**
     * 分页查找短链
     */
    @GetMapping("/page")
    public JsonData pageByGroupId(@RequestBody ShortLinkPageRequest request){
        return shortLinkService.pageByGroupId(request);
    }


}

